//-----------------------------------------------------------------------
//<copyright file="ThanhToanDAO.cs
//Code generate by tools.
//</copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using PMS.Exceptions;
using System.Data.SqlClient;
using PMS.Entity;

namespace PMS.DAL
{
	public class ThanhToanDAO : SQLDatabase, IThanhToanDAO
	{

		#region Create ThanhToan methods

		public ThanhToan CreateThanhToan(ThanhToan thanhToan)
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spThanhToanInsert");
				AddOutParameter(command, "@MaTT", SqlDbType.NVarChar, thanhToan.MaTT);
				AddInParameter(command, "@NgayTT", SqlDbType.DateTime, thanhToan.NgayTT);
				AddInParameter(command, "@PhuongThucTT", SqlDbType.NVarChar, thanhToan.PhuongThucTT);
				AddInParameter(command, "@SoTien", SqlDbType.Int, thanhToan.SoTien);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, thanhToan.GhiChu);
				AddInParameter(command, "@MaCTPX", SqlDbType.NVarChar, thanhToan.MaCTPX);

				ExecuteNonQuery(command);
                thanhToan.MaTT = (string)GetParameterValue(command, "@MaTT");
				return thanhToan;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access CreateThanhToanException", ex);
			}
		}

		#endregion

		#region Update ThanhToan methods

		public void UpdateThanhToan(ThanhToan thanhToan)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spThanhToanUpdate");

				AddInParameter(command, "@MaTT", SqlDbType.NVarChar, thanhToan.MaTT);
				AddInParameter(command, "@NgayTT", SqlDbType.DateTime, thanhToan.NgayTT);
				AddInParameter(command, "@PhuongThucTT", SqlDbType.NVarChar, thanhToan.PhuongThucTT);
				AddInParameter(command, "@SoTien", SqlDbType.Int, thanhToan.SoTien);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, thanhToan.GhiChu);
				AddInParameter(command, "@MaCTPX", SqlDbType.NVarChar, thanhToan.MaCTPX);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access UpdateThanhToanException", ex);
			}
		}

		#endregion

		#region Delete ThanhToan methods

		public void DeleteThanhToan(ThanhToan thanhToan)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spThanhToanDelete");

				AddInParameter(command, "@MaTT", SqlDbType.NVarChar, thanhToan.MaTT);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access DeleteThanhToanException", ex);
			}
		}

		#endregion

		#region Select methods

		#region GetAll ThanhToan methods

		public ThanhToans GetAllThanhToan()
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spThanhToanGetList");
				ThanhToans Result = new ThanhToans();
				List<ThanhToan> List = ExecuteQuery<ThanhToan>(command,true);
				foreach(ThanhToan item in List)
				{
					Result.Add(item);
				}
				return Result;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetAllThanhToanException", ex);
			}
		}

        public ThanhToans GetAllThanhToanBySoPX(string soPX)
        {
            try
            {

                SqlCommand command = SQLDatabase.StoredProcedureCommand("spGetAllThanhToanBySoPX");
                AddInParameter(command, "@SoPX", SqlDbType.NVarChar, soPX);
                ThanhToans Result = new ThanhToans();
                List<ThanhToan> List = ExecuteQuery<ThanhToan>(command, true);
                foreach (ThanhToan item in List)
                {
                    Result.Add(item);
                }
                return Result;
            }
            catch (Exception ex)
            {
                throw DataAccessException.GetException("Data Access GetAllThanhToanException", ex);
            }
        }

		#endregion


		#region GetThanhToan methods

		public ThanhToan GetThanhToanByID(string maTT)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spThanhToanGet");

				AddInParameter(command, "@maTT", SqlDbType.NVarChar, maTT);

				return ExecuteQuery<ThanhToan>(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetThanhToanBy ID Exception", ex);
			}
		}

		#endregion


		#endregion
	}
}//End Class
